/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package modulos.certificado.vista;

import basedatos.CertificadoBD;
import basedatos.OperacionesBD;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import modelo.Pyme;
import modulos.agenda.vista.iFrmAgenda;
import modulos.certificado.modelo.Certificado;
import modulos.paciente.modelo.Paciente;
import modulos.usuario.modelo.Usuario;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import net.sf.jasperreports.view.JasperViewer;
import vista.frmPrincipal;

/**
 *
 * @author victor
 */
public class iFrmCertificadoMedico extends javax.swing.JInternalFrame {

    private static Usuario usuario = new Usuario();
    private static Paciente paciente = new Paciente();
    static String [] d={"Nro.","Fecha"};
    private static DefaultTableModel modelotblCertificados = new DefaultTableModel(d,0);
    private static ArrayList<Certificado> listCertificados = new ArrayList<Certificado>();

    /**
     * Creates new form iFrmCertificadoMedico
     */
    public iFrmCertificadoMedico(Paciente paciente) {
        initComponents();
 
        usuario = (Usuario) frmPrincipal.UsuarioLogueado.get(0)[1];
        this.paciente = paciente;
        cargarCertificados();
    }

    public static void cargarCertificados() {

        int c = modelotblCertificados.getRowCount();
        try {
            for (int i = 0; i < c; i++) {
                modelotblCertificados.removeRow(0);
            }

        } catch (Exception ex) {
            ex.getMessage();
        }
        int cont = 0;
        DateFormat d=DateFormat.getDateInstance(DateFormat.SHORT);
        listCertificados = (ArrayList<Certificado>) CertificadoBD.listar(usuario, paciente);
        for (Certificado cert : listCertificados) {
            cont++;
            Object[] aux1 = {cont,
                d.format(cert.getFecha())
            };
            modelotblCertificados.addRow(aux1);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCerrar = new javax.swing.JButton();
        btnImprimir = new javax.swing.JButton();
        btnNuevo = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblCertificados = new JTable() {
            public boolean isCellEditable(int rowIndex, int vColIndex) {
                return false;
            }
        };
        btnEditar = new javax.swing.JButton();
        btnEliminar = new javax.swing.JButton();

        setClosable(true);
        setTitle("Certificado Medico");
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/Certificado Medico.png"))); // NOI18N

        btnCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/agt_home.png"))); // NOI18N
        btnCerrar.setText("Cerrar");
        btnCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCerrarActionPerformed(evt);
            }
        });

        btnImprimir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/printer1.png"))); // NOI18N
        btnImprimir.setText("Imprimir");
        btnImprimir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnImprimirActionPerformed(evt);
            }
        });

        btnNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/add.png"))); // NOI18N
        btnNuevo.setText("Nuevo");
        btnNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNuevoActionPerformed(evt);
            }
        });

        tblCertificados.setModel(modelotblCertificados);
        jScrollPane2.setViewportView(tblCertificados);

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/icono_editar.png"))); // NOI18N
        btnEditar.setText("Editar");

        btnEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/jpgs/delete.png"))); // NOI18N
        btnEliminar.setText("Eliminar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 727, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnImprimir, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnNuevo, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnEditar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(112, 112, 112)
                        .addComponent(btnCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 104, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCerrar)
                    .addComponent(btnImprimir)
                    .addComponent(btnNuevo)
                    .addComponent(btnEditar)
                    .addComponent(btnEliminar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCerrarActionPerformed
        dispose();
    }//GEN-LAST:event_btnCerrarActionPerformed

    private void btnNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNuevoActionPerformed
        iFrmCMNuevo aux = new iFrmCMNuevo(paciente);
        frmPrincipal.dskpPrincipal.add(aux);
        aux.setVisible(true);
    }//GEN-LAST:event_btnNuevoActionPerformed

    private void btnImprimirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnImprimirActionPerformed
        //Cargas Datos de Compañia
        int index = tblCertificados.getSelectedRow();
        if (index != -1) {

            ArrayList<Pyme> list = (ArrayList<Pyme>) OperacionesBD.listar("Pyme");
            Pyme pyme = null;
            if (!list.isEmpty()) {
                pyme = list.get(0);
            } else {
                pyme = new Pyme();
            }
            Certificado certificado = listCertificados.get(index);
            DateFormat d = DateFormat.getDateInstance(DateFormat.LONG);
            String fecha = d.format(certificado.getFecha());
            String medico = "";
            if (usuario.getGenero().equalsIgnoreCase("Femenino")) {
                medico = "Dra. " + usuario.getMedico();
            } else {
                medico = "Dr. " + usuario.getMedico();
            }
String pacient="";
        if(paciente.getGenero().equalsIgnoreCase("Masculino")){
            pacient="Que el paciente Sr. "+paciente;
        }else{
            pacient="Que la paciente Sra. "+ paciente;
        }
            Map<String, Object> parametros = new HashMap<String, Object>();
            parametros.put("COMPANIA", pyme.getNombre());
            parametros.put("FECHA", fecha);
            parametros.put("CONTENIDO", certificado.getContenido());
            parametros.put("MEDICO", medico);
            parametros.put("PACIENTE", pacient);
            parametros.put("CEDULA", paciente.getCedula());
                   parametros.put("ESPECIALIDAD",usuario.getEspecialidad());


            ArrayList aux = new ArrayList();
            aux.add("aux");

            try {
                JasperReport reporte = (JasperReport) JRLoader.loadObject(getClass().getResource("/modulos/reportes/certificado/Certificado.jasper"));

                JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, parametros, new JRBeanCollectionDataSource(aux));
                JasperViewer vista = new JasperViewer(jasperPrint, false);
                vista.setSize(frmPrincipal.dskpPrincipal.size());
                vista.setVisible(true);
            } catch (JRException ex) {
                Logger.getLogger(iFrmAgenda.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            JOptionPane.showMessageDialog(this, "Selecione el certificado", "Informacion", JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_btnImprimirActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCerrar;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JButton btnImprimir;
    private javax.swing.JButton btnNuevo;
    private javax.swing.JScrollPane jScrollPane2;
    private static javax.swing.JTable tblCertificados;
    // End of variables declaration//GEN-END:variables
}
